﻿namespace OleDbDAL.Systems
{
    using DBUtility;
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Configuration;
    using System.Data;
    using System.Text;

    public class WorkLogApp : IWorkLog
    {
        private string dbname = "Logs";

        public void Add(WorkLogEnt model)
        {
            string str = ConfigurationManager.AppSettings["IsUseLog"];
            if (str.ToLower() != "false")
            {
                StringBuilder builder = new StringBuilder();
                builder.Append("insert into T_WorkLog(");
                builder.Append("ChainNum,LoginName,UserName,WorkType,WorkNote,CreateTime,IpAddress");
                builder.Append(")");
                builder.Append(" values (");
                builder.Append("" + model.ChainNum + ",");
                builder.Append("'" + model.LoginName + "',");
                builder.Append("'" + model.UserName + "',");
                builder.Append("'" + model.WorkType + "',");
                builder.Append("'" + model.WorkNote + "',");
                builder.Append("'" + model.CreateTime + "',");
                builder.Append("'" + model.IpAddress + "'");
                builder.Append(")");
                DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
            }
        }

        public void ClearLog()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_WorkLog ");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int WorkId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_WorkLog ");
            builder.Append(" where WorkId=" + WorkId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_WorkLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by WorkId desc");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperOledb.GetMaxID(this.dbname, "WorkId", "T_WorkLog", "");
        }

        public WorkLogEnt GetModel(int WorkId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select WorkId,ChainNum,LoginName,UserName,WorkType,WorkNote,CreateTime,IpAddress from T_WorkLog ");
            builder.Append(" where WorkId=" + WorkId);
            WorkLogEnt ent = new WorkLogEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.WorkId = WorkId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.LoginName = table.Rows[0]["LoginName"].ToString();
                ent.UserName = table.Rows[0]["UserName"].ToString();
                ent.WorkType = table.Rows[0]["WorkType"].ToString();
                ent.WorkNote = table.Rows[0]["WorkNote"].ToString();
                if (table.Rows[0]["CreateTime"].ToString() != "")
                {
                    ent.CreateTime = DateTime.Parse(table.Rows[0]["CreateTime"].ToString());
                }
                ent.IpAddress = table.Rows[0]["IpAddress"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "WorkId desc";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_WorkLog ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "WorkId");
        }

        public void Update(WorkLogEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_WorkLog set ");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("LoginName='" + model.LoginName + "',");
            builder.Append("UserName='" + model.UserName + "',");
            builder.Append("WorkType='" + model.WorkType + "',");
            builder.Append("WorkNote='" + model.WorkNote + "',");
            builder.Append("CreateTime='" + model.CreateTime + "',");
            builder.Append("IpAddress='" + model.IpAddress + "'");
            builder.Append(" where WorkId=" + model.WorkId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

